﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WowGuildSite.Armory
{
    public struct ArmoryItemId
    {
        private int     mainId;
        private int?    randomPropertiesId;

        public static implicit operator ArmoryItemId(string idString)
        {
            ArmoryItemId newId = new ArmoryItemId();
            ParseInternal(idString,out newId.mainId,out newId.randomPropertiesId );
            return newId;
        }

        public static implicit operator ArmoryItemId(int id)
        {
            return new ArmoryItemId(id);
        }

        public static implicit operator string( ArmoryItemId id )
        {
            return id.ToString();
        }

        public bool             IsValid
        {
            get
            {
                if ( mainId >= 0 )
                {
                    if ( randomPropertiesId.HasValue )
                    {
                        return randomPropertiesId.Value >= 0;
                    }
                    return true;
                }
                return false;
            }
        }

        public int              MainId
        {
            get { return mainId; }
        }

        public bool             HasRandomProperties
        {
            get { return randomPropertiesId.HasValue; }
        }

        public int              RandomPropertiesId
        {
            get { return randomPropertiesId.Value; }
        }

        public override string  ToString()
        {
            if ( randomPropertiesId.HasValue )
            {
                return mainId.ToString() + ":" + randomPropertiesId.Value.ToString();
            }
            else
            {
                return mainId.ToString();
            }
        }

        public override int     GetHashCode()
        {
            return ToString().GetHashCode();
        }

        public override bool    Equals(object obj)
        {
            ArmoryItemId other = (ArmoryItemId) obj;

            return mainId.Equals(other.mainId) && randomPropertiesId.Equals(other.randomPropertiesId);
        }
    
        private static void ParseInternal( string input,out int mid,out int ? rpid )
        {
            if ( string.IsNullOrEmpty(input) )
            {
                mid  = -1;
                rpid = null;
                return;
            }

            string[] parts = input.Split(':');

            if ( parts != null && parts.Length == 2 )
            {
                mid     = int.Parse(parts[0]);
                rpid    = int.Parse(parts[1]);
            }
            else
            {
                mid     = int.Parse(input);
                rpid    = null;
            }
        }

        public ArmoryItemId( int mainId )
        {
            this.mainId             = mainId;
            this.randomPropertiesId = null;
        }

        public ArmoryItemId( int mainId, int randomPropertiesId )
        {
            this.mainId             = mainId;
            this.randomPropertiesId = randomPropertiesId;
        }

        public ArmoryItemId( string idString )
        {
            ParseInternal(idString,out mainId,out randomPropertiesId);
        }
    }
}
